//*****************************************************************************
// 1. Års eksamen @ Datamatiker - EASJ
// Holmegaard Heldagsskole Administrations system 
// 
// Denne klasse er udarbejdet af: Marc, Kasper og Nikolaj
//*****************************************************************************
package view.elev;

import control.ControlUtil;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.DefaultTableModel;
import model.Elev;
import model.ElevLog;
import model.ElevLogId;

public class LogbogPanel extends javax.swing.JPanel {

    private boolean opret;
    private ControlUtil controlUtil;
    private DefaultTableModel tabelModel;
    private ArrayList<ElevLog> elevLogs;

    public LogbogPanel(ControlUtil controlUtil) {
        this.controlUtil = controlUtil;
        elevLogs = new ArrayList<ElevLog>();
        opret = false;
        String col[] = {"Opretttelses Dato", "Hændelse", "Oprettet af"};
        String data[][] = null;
        tabelModel = new DefaultTableModel(data, col) {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        initComponents();


    }

    public void hentElever() {

        int value = vaelgElev.getSelectedIndex();
        vaelgElev.removeAllItems();
        vaelgElev.setEnabled(true);
        vaelgElev.addItem("Vælg elev");
        Elev[] el = controlUtil.getElevHandler().hentElever();
        for (int i = 0; i < el.length; i++) {
            vaelgElev.addItem(el[i]);
        }
        vaelgElev.setSelectedIndex(value);
    }

    private void opdaterElev(Elev elev) {

        rydFelter();
        ElevLog[] elevLog = controlUtil.getElevHandler().hentElevLog(elev);

        for (int i = 0; i < elevLog.length; i++) {
            elevLogs.add(elevLog[i]);
            tabelModel.insertRow(tabelModel.getRowCount(), new Object[]{elevLog[i].getDate(), elevLog[i].getId().getLog(), elevLog[i].getPersonale().getFornavn() + " " + elevLog[i].getPersonale().getEfternavn()});
        }
    }

    private void nulstil() {

        opret = false;
        hentElever();
        haendelse.setText("");
        opretKnap.setText("Gem hændelse");
        haendelse.setEnabled(true);
    }

    private void rydFelter() {
        while (tabelModel.getRowCount() > 0) {
            tabelModel.removeRow(0);
            elevLogs.remove(0);
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        vaelgElev = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tabel = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        haendelse = new javax.swing.JTextArea();
        opretKnap = new javax.swing.JButton();
        sletKnap = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(1016, 696));
        setMinimumSize(new java.awt.Dimension(1016, 696));
        setPreferredSize(new java.awt.Dimension(1016, 696));
        setLayout(null);

        jLabel1.setText("Vælg Elev:");
        add(jLabel1);
        jLabel1.setBounds(10, 20, 160, 14);

        vaelgElev.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg elev" }));
        vaelgElev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vaelgElevActionPerformed(evt);
            }
        });
        add(vaelgElev);
        vaelgElev.setBounds(10, 40, 163, 20);

        Tabel.setModel(tabelModel);
        Tabel.getTableHeader().setReorderingAllowed(false);
        Tabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TabelMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(Tabel);

        add(jScrollPane1);
        jScrollPane1.setBounds(10, 80, 1000, 310);

        jLabel2.setText("Ny Hændelse:");
        add(jLabel2);
        jLabel2.setBounds(10, 400, 80, 14);

        haendelse.setColumns(20);
        haendelse.setRows(5);
        jScrollPane2.setViewportView(haendelse);

        add(jScrollPane2);
        jScrollPane2.setBounds(10, 420, 400, 120);

        opretKnap.setText("Gem hændelse");
        opretKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                opretKnapActionPerformed(evt);
            }
        });
        add(opretKnap);
        opretKnap.setBounds(430, 430, 130, 50);

        sletKnap.setText("Slet hændelse");
        sletKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sletKnapActionPerformed(evt);
            }
        });
        add(sletKnap);
        sletKnap.setBounds(430, 490, 130, 50);
    }// </editor-fold>//GEN-END:initComponents

    private void vaelgElevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vaelgElevActionPerformed
        if (vaelgElev.getItemCount() != 0 && vaelgElev.getSelectedIndex() != 0) {
            opdaterElev((Elev) vaelgElev.getSelectedItem());
        } else {
            rydFelter();
        }
    }//GEN-LAST:event_vaelgElevActionPerformed

    private void sletKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sletKnapActionPerformed
        if (Tabel.getSelectedRow() >= 0) {
            controlUtil.getElevHandler().sletLog(elevLogs.get(Tabel.getSelectedRow()));
            hentElever();
            opdaterElev((Elev) vaelgElev.getSelectedItem());
        }
        nulstil();
    }//GEN-LAST:event_sletKnapActionPerformed

    private void opretKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_opretKnapActionPerformed

        if (opret == false) {
            Elev elev = (Elev) vaelgElev.getSelectedItem();
            ElevLog log = new ElevLog(new ElevLogId(elev.getCpr(), controlUtil.getBruger().getPersonaleId(), haendelse.getText()), elev, controlUtil.getBruger(), new Date());
            controlUtil.getElevHandler().nyLog(log);
            hentElever();
            opdaterElev(elev);
            haendelse.setText("");
        }
        nulstil();
    }//GEN-LAST:event_opretKnapActionPerformed

    private void TabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TabelMouseClicked
        if (Tabel.getSelectedRow() >= 0) {
            opret = true;
            opretKnap.setText("Opret ny hændelse");
            haendelse.setText(elevLogs.get(Tabel.getSelectedRow()).toString());
            haendelse.setEnabled(false);
        }
    }//GEN-LAST:event_TabelMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Tabel;
    private javax.swing.JTextArea haendelse;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton opretKnap;
    private javax.swing.JButton sletKnap;
    private javax.swing.JComboBox vaelgElev;
    // End of variables declaration//GEN-END:variables
}
